using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace msgExchange
{

    public class CSysSettings
    {
        public CSysSettings() { }

        public string ipAddress_="127.0.0.1";
        public int port_=8567;

        public bool enableWelcameFlag_ = false;
        public string dataWelcame_ = "Hello!";

        public bool enableTracingGuestAddress_= false;

        public int deadlineTimeInterval_=20;
        public string chatDataSavePath_ = "";

        public string account_ = "";
        public string pwd_= "1";

        public bool enableAutoBindingDomain_ = false;
        public bool enableNotifier_ = false;
    }

    public class CSysSettingMananger
    {
        const string PATH_REG_CLI_ = "Software\\clionline\\security";

        public CSysSettings setting_ = new CSysSettings();
        CRegistryWrapper rw_= new CRegistryWrapper(CRegistryWrapper.RootKeys.CURRENT_USER );
        public CSysSettingMananger() 
        {
           rw_.CreateRegKey(PATH_REG_CLI_);
        }

        public bool getPWDFlag()
        {
            bool ret = (rw_.GetRegVal(PATH_REG_CLI_, "PWDFlag") == "1");
            return ret;
        }
        public void setPWDFlag(bool v)
        {
            rw_.SetRegVal(PATH_REG_CLI_, "PWDFlag", v ? "1" : "0");
        }

        public string getPWD()
        {
            return rw_.GetRegVal(PATH_REG_CLI_, "PWD");
        }

        public void setPWD(string pwd)
        {
            rw_.SetRegVal(PATH_REG_CLI_, "PWD",pwd);
        }

        public bool save()
        {
            try
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

                config.AppSettings.Settings["SERVERIP"].Value = setting_.ipAddress_ ; 
                config.AppSettings.Settings["SERVERPORT"].Value = setting_.port_.ToString() ;

                config.AppSettings.Settings["ENABLEWELCAMEFLAG"].Value = setting_.enableWelcameFlag_ ? "1" : "0";
                config.AppSettings.Settings["DATAWELCAME"].Value = setting_.dataWelcame_.ToString() ;
                config.AppSettings.Settings["ENABLETRACINGGUESTADDRESS"].Value = setting_.enableTracingGuestAddress_ ? "1" : "0";
                config.AppSettings.Settings["DEADLINETIMEINTERVAL"].Value = setting_.deadlineTimeInterval_.ToString() ; 
                config.AppSettings.Settings["CHATDATASAVEPATH"].Value = setting_.chatDataSavePath_.ToString() ;
                config.AppSettings.Settings["AUTOBINDDOMAIN"].Value = setting_.enableAutoBindingDomain_ ? "1" : "0";
                config.AppSettings.Settings["ENABLENOTIFIER"].Value = setting_.enableNotifier_ ? "1" : "0";

                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("AppSettings");

                return true;
            }
            catch { }

            return false;
        }

        public bool load()
        {
            try
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

                foreach (string v in config.AppSettings.Settings.AllKeys)
                {
                    string val=config.AppSettings.Settings[v].Value.ToString();
                    if (v.ToUpper() == "SERVERIP")
                    {
                        setting_.ipAddress_ = val;
                    }
                    else if (v.ToUpper() == "SERVERPORT")
                    {
                        setting_.port_ = Convert.ToInt32(val);
                    }
                    else if (v.ToUpper() == "ENABLEWELCAMEFLAG")
                    {
                        setting_.enableWelcameFlag_ = ("1" == val);
                    }
                    else if (v.ToUpper() == "DATAWELCAME")
                    {
                        setting_.dataWelcame_ = val;
                    }
                    else if (v.ToUpper() == "ENABLETRACINGGUESTADDRESS")
                    {
                        setting_.enableTracingGuestAddress_ = ("1" == val);
                    }
                    else if (v.ToUpper() == "DEADLINETIMEINTERVAL")
                    {
                        setting_.deadlineTimeInterval_ = Convert.ToInt32(val);
                    }
                    else if (v.ToUpper() == "CHATDATASAVEPATH")
                    {
                        setting_.chatDataSavePath_ = val;
                    }
                    else if (v.ToUpper() == "AUTOBINDDOMAIN")
                    {
                        setting_.enableAutoBindingDomain_ = ("1" == val);
                    }
                    else if (v.ToUpper() == "ENABLENOTIFIER")
                    {
                        setting_.enableNotifier_ = ("1" == val);
                    }

                }

                return true;
            }
            catch (Exception e)
            {

            }

            return false;
        }

    }

}
